/**
 * @file frame.c
 * @author your name (you@domain.com)
 * @brief  ui frame
 * @version 0.1
 * @date 2023-06-01
 * 
 * @copyright Copyright (c) 2023
 * 
 */
#include "frame.h"
#include <stdbool.h>

typedef struct easy_ui_frame
{
    MsgCenterHandle_t *msg_center_handle;

}easy_ui_frame_t;

static easy_ui_frame_t s_easy_ui_frame = {0};
static bool s_already_init = false;

int easy_ui_frame_init()
{
    if(s_already_init)
    {
        return 0;
    }

    s_easy_ui_frame.msg_center_handle = MsgCenterInit(NULL, NULL);
    if(s_easy_ui_frame.msg_center_handle == NULL)
    {
        return -1;
    }

    return 0;
}

int easy_ui_frame_loop()
{
    if(s_easy_ui_frame.msg_center_handle != NULL)
    {
        MsgCenterLoop(s_easy_ui_frame.msg_center_handle);
        return 0;
    }
    return -1;
}

int easy_ui_frame_deinit()
{
    if(s_easy_ui_frame.msg_center_handle != NULL)
       MsgCenterDeinit(&s_easy_ui_frame.msg_center_handle);
    s_easy_ui_frame.msg_center_handle = NULL;
    return 0;
}

int easy_ui_pub_event(char *event, void *data, int datalen, int srcId)
{
    if(s_easy_ui_frame.msg_center_handle != NULL)
    {
        PublishEvent(s_easy_ui_frame.msg_center_handle, event, data, datalen, srcId);
        return 0;
    }

    return -1;
}

int easy_ui_sub_event(char *event, HandleEvent func, int needId)
{
    if(s_easy_ui_frame.msg_center_handle != NULL)
    {
        SubscriberEvent(s_easy_ui_frame.msg_center_handle, event, func, needId);
        return 0;
    }

    return -1;
}

int easy_ui_sub_event_cancel(char *event, HandleEvent func)
{
    if(s_easy_ui_frame.msg_center_handle != NULL)
    {
        ReleaseSubscriberEvent(s_easy_ui_frame.msg_center_handle, event, func);
        return 0;
    }

    return -1;
}
